package com.jiang.myfm.adapters;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jiang.myfm.MainActivity;
import com.jiang.myfm.R;
import com.jiang.myfm.entities.mainpagetwo.DataListHotRecommends;
import com.jiang.myfm.entities.mainpagetwo.ListHotRecommends;
import com.jiang.myfm.newview.CustomRelativeLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dell on 2016/12/9 0009.
 */
public class MainRecyclerAdapterMore extends RecyclerView.Adapter<MainRecyclerAdapterMore.ViewHolder> {
    private List<ListHotRecommends> mListHotRecommends;
    private Context context;
    private List<DataListHotRecommends> list = new ArrayList<>();
    private MainRecyclerAdapterMoreItem mMainRecyclerAdapterMoreItem;

    public MainRecyclerAdapterMore(List<ListHotRecommends> listHotRecommends, Context context) {
        mListHotRecommends = listHotRecommends;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_main_recycler_more, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mListHotRecommends.get(position).getTitle());
        initItem(holder, position);
    }

    private void initItem(ViewHolder holder, int position) {
        list.addAll(mListHotRecommends.get(position).getList());
        List<DataListHotRecommends> listSix=new ArrayList<>();
        for (int i = 0; i < mListHotRecommends.get(position).getList().size(); i++) {
            if (i<6){
                listSix.add(mListHotRecommends.get(position).getList().get(i));
            }
        }
        mMainRecyclerAdapterMoreItem = new MainRecyclerAdapterMoreItem(listSix,context);
        holder.mRecyclerView.setAdapter(mMainRecyclerAdapterMoreItem);
        GridLayoutManager managerMore = new GridLayoutManager(context,3);
        holder.mRecyclerView.setLayoutManager(managerMore);
    }

    @Override
    public int getItemCount() {
        return mListHotRecommends != null ? mListHotRecommends.size() : 0;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private CustomRelativeLayout mCustomRelativeLayout;
        private TextView mTextView;
        private RecyclerView mRecyclerView;

        public ViewHolder(View itemView) {
            super(itemView);
            mCustomRelativeLayout = (CustomRelativeLayout) itemView.findViewById(R.id.find_1_title_more_item);
            mCustomRelativeLayout.setMaxHeight(MainActivity.MAX_HEIGHT_TITLE);
            mTextView = (TextView) itemView.findViewById(R.id.find_1_title_more_tv1_item);
            mRecyclerView = (RecyclerView) itemView.findViewById(R.id.find_1_recycler_view_more_item);
        }
    }

    public void addAll(List<ListHotRecommends> list) {
        mListHotRecommends.addAll(list);
        notifyDataSetChanged();
    }
}
